package com.im.tcp.publish;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.im.codec.proto.Message;
import com.im.codec.proto.MessageHeader;
import com.im.common.constant.Constants;
import com.im.common.enums.command.CommandType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Mq消息发送器，用于向RabbitMQ发送消息
 */
@Slf4j
@Component
public class MqMessageProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息到消息队列
     *
     * @param message 要发送的消息
     * @param command 消息类型
     */
    public Object sendMessage(Message message, Integer command) {
        String com = command.toString();
        String commandSub = com.substring(0, 1);
        CommandType commandType = CommandType.getCommandType(commandSub);
        String channelName = "";
        // 根据命令类型确定消息所属的通道名称
        if (commandType == CommandType.MESSAGE) {
            channelName = Constants.RabbitConstants.Im2MessageService;
        } else if (commandType == CommandType.GROUP) {
            channelName = Constants.RabbitConstants.Im2GroupService;
        } else if (commandType == CommandType.FRIEND) {
            channelName = Constants.RabbitConstants.Im2FriendshipService;
        } else if (commandType == CommandType.USER) {
            channelName = Constants.RabbitConstants.Im2UserService;
        }
        // 将消息转换成 JSON 格式，并添加消息类型、客户端类型、设备IMEI号和应用ID
        JSONObject o = (JSONObject) JSON.toJSON(message.getMessagePack());
        o.put("command", command);
        o.put("clientType", message.getMessageHeader().getClientType());
        o.put("imei", message.getMessageHeader().getImei());
        o.put("appId", message.getMessageHeader().getAppId());
        try {
            // 发送消息到 RabbitMQ
            rabbitTemplate.convertAndSend(channelName, "", o.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送mq消息异常：{}", e.getMessage());
        }
        return o;
    }

    /**
     * 发送消息到消息队列
     *
     * @param message 要发送的消息
     * @param header  消息头
     * @param command 消息类型
     */
    public Object sendMessage(Object message, MessageHeader header, Integer command) {
        String com = command.toString();
        String commandSub = com.substring(0, 1);
        CommandType commandType = CommandType.getCommandType(commandSub);
        String channelName = "";
        // 根据命令类型确定消息所属的通道名称
        if (commandType == CommandType.MESSAGE) {
            channelName = Constants.RabbitConstants.Im2MessageService;
        } else if (commandType == CommandType.GROUP) {
            channelName = Constants.RabbitConstants.Im2GroupService;
        } else if (commandType == CommandType.FRIEND) {
            channelName = Constants.RabbitConstants.Im2FriendshipService;
        } else if (commandType == CommandType.USER) {
            channelName = Constants.RabbitConstants.Im2UserService;
        }
        // 将消息转换成 JSON 格式，并添加消息类型、客户端类型、设备IMEI号和应用ID
        JSONObject o = (JSONObject) JSON.toJSON(message);
        o.put("command", command);
        o.put("clientType", header.getClientType());
        o.put("imei", header.getImei());
        o.put("appId", header.getAppId());
        try {
            // 发送消息到 RabbitMQ
            rabbitTemplate.convertAndSend(channelName, "", o.toJSONString());
        } catch (Exception e) {
            log.error("发送mq消息异常：{}", e.getMessage());
            e.printStackTrace();
        }
        return o;
    }
}
